package team.knowers.qzh.qzhserver.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import team.knowers.qzh.qzhserver.common.convention.result.Result;
import team.knowers.qzh.qzhserver.common.convention.result.Results;
import team.knowers.qzh.qzhserver.common.database.BaseDO;
import team.knowers.qzh.qzhserver.dao.entity.SectionDO;
import team.knowers.qzh.qzhserver.dto.req.VersionLikeReqDTO;
import team.knowers.qzh.qzhserver.dto.resp.SectionDetailsRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.SectionRecentRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.VersionSectionRespDTO;
import team.knowers.qzh.qzhserver.service.LikeVersionLogsService;
import team.knowers.qzh.qzhserver.service.SectionService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wutai
 */
@RestController
@RequiredArgsConstructor
public class SectionController {

    private final SectionService sectionService;
    private final LikeVersionLogsService likeVersionLogsService;

    /**
     * 根据单元id，展示单元章节目录（作者原创）
     */
    @GetMapping("/api/qzh-server/admin/v1/section/details")
    public Result<List<SectionDetailsRespDTO>> getSectionDetailsById(@RequestParam("chapterId") Long id) {
        return Results.success(sectionService.getSectionDetailsById(id));
    }

    /**
     * 根据章节号显示该章节对应的所有版本
     */
    @GetMapping("/api/qzh-server/admin/v1/section/version")
    public Result<List<VersionSectionRespDTO>> listMultiVersionSectionBySectionNumber(@RequestParam("sectionNumber") Long number) {
        return Results.success(sectionService.listMultiVersionSectionBySectionNumber(number));
    }

    /**
     * 为当前版本点赞
     */
    @PostMapping("/api/qzh-server/admin/v1/section/version/like")
    public Result<Boolean> likeThisVersionSection(@RequestBody VersionLikeReqDTO reqDTO) {
        return Results.success(likeVersionLogsService.likeThisVersionSection(reqDTO));
    }

    /**
     * 用户最近发布版本
     */
    @GetMapping("/api/qzh-server/admin/v1/section/recent")
    public Result<List<SectionRecentRespDTO>> homeUserVersionByTimeDesc(@RequestParam("userId") Long userId) {
        return Results.success(sectionService.homeUserVersionByTimeDesc(userId));
    }
}
